package controller

import (
	"github.com/gin-gonic/gin"
	"kf/constant"
	"kf/models/dao"
	"kf/tools"
	"kf/utils/R"
	"net/http"
	"time"
)

type LoginController struct {
	AuthController *AuthController
	SysMenuDao     *dao.SysMenuDao
	SysUserDao     *dao.SysUserDao
}

// 增加了验证码的
func (loginController *LoginController) LoginCheckPassV1(c *gin.Context) {
	password := c.PostForm("password")
	username := c.PostForm("username")
	captchaKey := c.PostForm("captchaKey")
	captchaCode := c.PostForm("captchaCode")
	//校验验证码
	captcha := tools.VerifyCaptcha(captchaKey, captchaCode)
	if !captcha {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "验证码不正确",
		})
		return
	}
	//校验密码
	info, uRole, ok := loginController.AuthController.CheckKefuPass(username, password)

	if !ok {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户名或密码不正确",
		})
		return
	}
	//来似创建用户token
	userinfo := make(map[string]any)
	userinfo[constant.UserInfo] = info
	userinfo[constant.NikeName] = info.NickName
	userinfo[constant.UserName] = info.UserName
	userinfo[constant.UserId] = info.ID
	userinfo["type"] = "kefu"
	if uRole.RoleId != 0 {
		userinfo["role_id"] = uRole.RoleId
	} else {
		userinfo["role_id"] = 2
	}
	userinfo["create_time"] = time.Now().Unix()

	token, _ := tools.MakeToken(userinfo)
	userinfo["ref_token"] = true
	refToken, _ := tools.MakeToken(userinfo)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "验证成功,正在跳转",
		"data": gin.H{
			"accessToken":  token,
			"refreshToken": refToken,
			"tokenType":    "Bearer",
			"expires":      nil,
			"create_time":  userinfo["create_time"],
		},
	})
}

func (loginController *LoginController) Logout(c *gin.Context) {
	c.JSON(http.StatusOK, R.ReturnSuccess("退出成功"))
}
